package jee.demo.pro.dao.impl;

import jee.demo.pro.dao.AbstractDAO;
import jee.demo.pro.dao.UserDAO;
import jee.demo.pro.model.User;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

/**
 * Created by Zhen on 2015/4/17.
 */
@Repository
@Transactional
@NamedQueries({
        @NamedQuery(name = "logout", query = "update User set online = false where username = :username"),
        @NamedQuery(name = "register", query = "from User where username = ? or nickname = ? or email = ?"),
        @NamedQuery(name = "check_register", query = "select (select count(*) from User where username= ?), (select count(*) from User where nickname=?) as inickname, count(*) from User u where email = ?")
})
public class UserDAOImpl extends AbstractDAO<User> implements UserDAO {
    public UserDAOImpl() {
        super("User", User.class);
    }

    @Override
    public User getByUsername(String username) {
        return uniqueByProperty("username", username);
    }

    @Override
    public User getByUsername(User user) {
        return uniqueByProperty("username", user.getUsername());
    }

    @Override
    public Long countByUsername(String username) {
        return countByProperty("username", username);
    }

    @Override
    public Long countByNickname(String nickname) {
        return countByProperty("nickname", nickname);
    }

    @Override
    public void login(User user) {
        //updateByNamedQuery("login", new Object[]{user.lastlogin, user.lasthost, user.id});
    }

    @Override
    public void logout(Integer id) {
        updateByNamedQuery("logout", id);
    }

}
